<?php
/**
 * 用户
 *
 *
 */


class cart_model extends CI_Model
{
	/**
	 $cart数据结构
		1
			restaurantID
			name
			foodArray
			{
			1
			    foodID
			    price
			    name
			    quantity
			    
			2
			    foodID
			    price
			    name
			    quantity
			    subtotoal
			} 
	 */
	
	private $cart;
	private static $food;
	private $INFINITY = 10000;
	
	function __construct()
	{
		//因为ci的session取的不是引用，更改后，是副本的更改。因此这里统一却出，然后所有函数都要调用getCartAndSetSession来更新session的cart
		$this->cart = $this->session->userdata('cart');
		if($this->cart == null)
		{
			$this->cart = array();
		}
		$this->printCartSession();
	}
	
	/**
	 * 
	 * 添加价格
	 */
	private function fillTotalPrice()
	{
		foreach($this->cart as $restaurantIndex => &$oneRestaurantCart )
		{
			$totalPrice = 0;
			foreach ($oneRestaurantCart['foodArray'] as $oneCartfood)
			{
				$totalPrice += $oneCartfood['price']* $oneCartfood['quantity'];
			}
			$oneRestaurantCart['restaurantTotalPrice'] = $totalPrice;
		}
	}
	
	/**
	 * 
	 * 返回购物车的价格总计
	 */
	function getTotalPrice()
	{
		$totalPrice = 0;
		foreach($this->cart as $oneRestaurantCart )
		{
			foreach ($oneRestaurantCart['foodArray'] as $oneCartfood)
			{
				$totalPrice += $oneCartfood['price']* $oneCartfood['quantity'];
			}
		}
		return $totalPrice;
	}
	
	function getTotalPriceByRestaurantID($restaurantID)
	{
		$totalPrice = 0;
		foreach($this->cart as $oneRestaurantCart )
		{
			if($oneRestaurantCart['restaurantID'] == $restaurantID)
			{
				foreach ($oneRestaurantCart['foodArray'] as $oneCartfood)
				{
					$totalPrice += $oneCartfood['price']* $oneCartfood['quantity'];
				}
				break;
			}
		}
		return $totalPrice;
	}
	
	/**
	 * 
	 * 根据指定foodid或者小计价格。对不存在foodid返回0
	 * @param int $foodID
	 */
	function getSubTotal($foodID)
	{		
		foreach($this->cart as $oneRestaurantCart )
		{
			foreach ($oneRestaurantCart['foodArray'] as $oneCartfood)
			{
				if($oneCartfood['foodID'] == $foodID)
				{
					return $oneCartfood['price']* $oneCartfood['quantity'];
				}
			}
		}
		return 0;
	}

	/**
	 * 
	 * 判断cart中是否包含指定的restaurant
	 */
	function containRestaurant($restaurantID)
	{
		foreach($this->cart as $oneRestaurantCart )
		{
			if($oneRestaurantCart['restaurantID'] == $restaurantID )
			{
				return true;
			}
		}
		return false;
	}
	
	function getCartInfo()
	{
		return $this->session->userdata('cart');
	}
	
	//必须统一到这里设置
	function getCartAndSetSession()
	{		
		$this->fillTotalPrice();
		$this->session->set_userdata('cart', $this->cart);
		return $this->cart;
	}
	
	function addFood($foodID, $quantity =1)
	{
		if($quantity <=0)
		{
			return;	
		}
		
		//取food信息
		$this->load->model('food_model');
		$food = $this->food_model->load($foodID);
		
		$restaurantID = $food['restaurantId'];
				
		//第一次加入指定restaurant的food时，先加载restaurant信息
		if( ! $this->containRestaurant($restaurantID) )
		{
			//取restaurant信息
			$this->load->model('restaurant_model');
			$restaurant = $this->restaurant_model->load($restaurantID);
			
			$restaurantCart = array();
			$restaurantCart['restaurantID'] = $restaurantID;
			$restaurantCart['name'] = $restaurant['name'];
			$restaurantCart['foodArray'] = array();
			$this->cart[] = $restaurantCart;
		}
		
		//找到指定的foodArray
		foreach($this->cart as $restaurantIndex => $oneRestaurant)
		{
			if($oneRestaurant['restaurantID'] == $restaurantID)
			{
				//引用
				$foodArray = &$this->cart[$restaurantIndex]['foodArray'];
			}
		}
		
		//如果有就更新quantity
		$findFoodFlag = false;
		foreach ($foodArray as $foodIndex => &$oneCartFood)
		{
			if($oneCartFood['foodID'] == $foodID)
			{
				$oneCartFood['quantity'] += $quantity;
				$findFoodFlag = true;
				break;
			}
		}
		
		//原来没有,加入food
		if($findFoodFlag == false)
		{
			$foodCart = array();
			$foodCart['foodID'] = $foodID;
			$foodCart['price'] = $food['unitPrice'];
			$foodCart['name'] = $food['name'];
			$foodCart['hasPicture'] = $food['hasPicture'];
			$foodCart['picture'] = $food['picture'];
			$foodCart['quantity'] = $quantity;
			$foodArray[] = $foodCart;
		}
				
		return $this->getCartAndSetSession();
	}
	
	function substractFood($foodID, $quantity =1)
	{
		if( $quantity <=0 )
		{
			return;
		}
		
		$findFlag = false;
	
		if($this->cart == null)
		{
			//在应用中，应该保证逻辑的严密性，不会发生下面两种错误
			echo "发生错误，减少物品时没有购物车";
			return null;
		}
		
		foreach($this->cart as &$oneRestaurantCart )
		{
			foreach ($oneRestaurantCart['foodArray'] as &$oneCartfood)
			{
				if($oneCartfood['foodID'] == $foodID)
				{
					$findFlag = true;
					$oldQuantity = $oneCartfood['quantity'];
					$newQuantity = $oldQuantity -= $quantity;
					$oneCartfood['quantity'] = $newQuantity;
					if($newQuantity <= 0)
					{
						$this->removeFood($foodID);
					}
					break;				
				}
			}
			if($findFlag == true)
			{
				break;
			}
		}
		
		if($findFlag == false)
		{
			echo "发生错误，减少购物车中没有的食物";
			return null;
		}		
		
		return $this->getCartAndSetSession();
	}
	
	private function printCartSession()
	{	
		return;
		echo "<br/>";
		print_r($this->cart);
		echo "<br/>";
		
	}
	
	function removeFood($foodID)
	{
		//使用$restaurantIndex,$foodIndex记录位置
		$findFoodFlag = false;
		foreach($this->cart as $restaurantIndex => &$oneRestaurantCart )
		{
			foreach ($oneRestaurantCart['foodArray'] as $foodIndex => $oneCartfood)
			{
				if($oneCartfood['foodID'] == $foodID)
				{
					$findFoodFlag = true;
					
					unset($this->cart[$restaurantIndex]['foodArray'][$foodIndex]);
					//如果foodArray为空，移除restaurant
					if(count($oneRestaurantCart['foodArray']) ==0 )
					{
						unset($this->cart[$restaurantIndex]);
					}
					break;
				}
			}
			
			if($findFoodFlag == true)
			{
				break;
			}
		}
		
					
		return $this->getCartAndSetSession();
	}
	
	function updateFood($foodID, $quantity)
	{
		if($quantity<0)
		{
			echo '$quantity 不能小于0\n';
			$quantity = 0;
		}
		$this->removeFood($foodID);
		$this->addFood($foodID, $quantity);
		return $this->getCartAndSetSession();
	}
	
	
	function clearCart()
	{		
		$this->cart = array();
		return $this->getCartAndSetSession();
	}
	function clearCartWhenChangeRestaurant($restaurantID){
		foreach($this->cart as $index=> $oneRestaurantCart)
		{
			if($oneRestaurantCart['restaurantID']!=$restaurantID)
			{
				unset($this->cart[$index]);
			}
		}
		$this->cart=array_values($this->cart);
		return $this->getCartAndSetSession();
	}
	
	function __destruct()
	{
		$this->printCartSession();
	}
}